package cn.dankal.flyaudio.chat;

import android.text.TextUtils;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;

import cn.dankal.flyaudio.friend.SortConvList;
import cn.dankal.flyaudio.friend.SortTopConvList;
import cn.dankal.flyaudio.friend.adapter.ConversationListAdapter;
import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.android.api.content.MessageContent;
import cn.jpush.im.android.api.enums.ConversationType;
import cn.jpush.im.android.api.exceptions.JMFileSizeExceedException;
import cn.jpush.im.android.api.model.Conversation;
import cn.jpush.im.android.api.model.GroupInfo;
import cn.jpush.im.android.api.model.Message;
import cn.jpush.im.android.api.model.UserInfo;
import cn.jpush.im.api.BasicCallback;

public class ConversationListHelper {
    private List<Conversation> conversations = new ArrayList<Conversation>();


    List<Conversation> topConv = new ArrayList<>();
    List<Conversation> forCurrent = new ArrayList<>();
    List<Conversation> delFeedBack = new ArrayList<>();

    public List<Conversation> getConversations() {
        return conversations;
    }

    public Conversation getSettingConversation(){
       return new Conversation() {
           @Override
           public ConversationType getType() {
               return ConversationType.chatroom;
           }

           @Override
            public String getTargetAppKey() {
                return null;
            }

            @Override
            public boolean setUnReadMessageCnt(int i) {
                return false;
            }

            @Override
            public String getTitle() {
                return "每天精彩推荐";
            }

            @Override
            public Message getLatestMessage() {
                return Message.fromJson("每天精彩推荐");
            }

            @Override
            public Object getTargetInfo() {
                return null;
            }

            @Override
            public boolean resetUnreadCount() {
                return false;
            }

            @Override
            public Message getMessage(int i) {
                return null;
            }

            @Override
            public Message getMessage(long l) {
                return null;
            }

            @Override
            public List<Message> getAllMessage() {
                return null;
            }

            @Override
            public List<Message> getMessagesFromOldest(int i, int i1) {
                return null;
            }

            @Override
            public List<Message> getMessagesFromNewest(int i, int i1) {
                return null;
            }

            @Override
            public boolean deleteMessage(int i) {
                return false;
            }

            @Override
            public boolean deleteAllMessage() {
                return false;
            }

            @Override
            public boolean updateConversationExtra(String s) {
                return false;
            }

            @Override
            public boolean updateMessageExtra(Message message, String s, String s1) {
                return false;
            }

            @Override
            public boolean updateMessageExtra(Message message, String s, Number number) {
                return false;
            }

            @Override
            public boolean updateMessageExtra(Message message, String s, Boolean aBoolean) {
                return false;
            }

            @Override
            public boolean updateMessageExtras(Message message, Map<String, String> map) {
                return false;
            }

            @Override
            public Message createSendMessage(MessageContent messageContent) {
                return null;
            }

            @Override
            public Message createSendMessage(MessageContent messageContent, String s) {
                return null;
            }

            @Override
            public Message createSendMessage(MessageContent messageContent, List<UserInfo> list, String s) {
                return null;
            }

            @Override
            public Message createSendMessageAtAllMember(MessageContent messageContent, String s) {
                return null;
            }

            @Override
            public Message createSendTextMessage(String s) {
                return null;
            }

            @Override
            public Message createSendTextMessage(String s, String s1) {
                return null;
            }

            @Override
            public Message createSendImageMessage(File file) throws FileNotFoundException {
                return null;
            }

            @Override
            public Message createSendImageMessage(File file, String s) throws FileNotFoundException {
                return null;
            }

            @Override
            public Message createSendVoiceMessage(File file, int i) throws FileNotFoundException {
                return null;
            }

            @Override
            public Message createSendVoiceMessage(File file, int i, String s) throws FileNotFoundException {
                return null;
            }

            @Override
            public Message createSendCustomMessage(Map<? extends String, ? extends String> map) {
                return null;
            }

            @Override
            public Message createSendCustomMessage(Map<? extends String, ? extends String> map, String s) {
                return null;
            }

            @Override
            public Message createSendFileMessage(File file, String s, String s1) throws FileNotFoundException, JMFileSizeExceedException {
                return null;
            }

            @Override
            public Message createSendFileMessage(File file, String s) throws FileNotFoundException, JMFileSizeExceedException {
                return null;
            }

            @Override
            public Message createLocationMessage(double v, double v1, int i, String s) {
                return null;
            }

            @Override
            public void retractMessage(Message message, BasicCallback basicCallback) {

            }
        };
    }


    public void initConvListAdapter() {
        forCurrent.clear();
        topConv.clear();
        delFeedBack.clear();
        int i = 0;
        conversations = JMessageClient.getConversationList();
        if (conversations != null && conversations.size() > 0) {
//            mConvListView.setNullConversation(true);
            SortConvList sortConvList = new SortConvList();
            Collections.sort(conversations, sortConvList);
            for (Conversation con : conversations) {
                //如果会话有聊天室会话就把这会话删除
                if (con.getTargetId().equals("feedback_Android") || con.getType().equals(ConversationType.chatroom)) {
                    delFeedBack.add(con);
                }
                if (!TextUtils.isEmpty(con.getExtra())) {
                    forCurrent.add(con);
                }
            }
            topConv.addAll(forCurrent);
            conversations.removeAll(forCurrent);
            conversations.removeAll(delFeedBack);
        } else {
            //空会话列表
//            mConvListView.setNullConversation(false);
        }
        if (topConv != null && topConv.size() > 0) {
            SortTopConvList top = new SortTopConvList();
            Collections.sort(topConv, top);
            for (Conversation conv : topConv) {
                conversations.add(i, conv);
                i++;
            }
        }
//        mListAdapter = new ConversationListAdapter(mContext.getActivity(), mDatas, mConvListView);
//        mConvListView.setConvListAdapter(mListAdapter);
    }

}
